package com.twu.biblioteca;

import java.io.IOException;

public class Biblioteca {
    private Librarian librarian = new Librarian();
    private Customer customer = new Customer();
    private User user = null;

    public static void main(String args[]) throws IOException {
        Biblioteca biblioteca = new Biblioteca();
        while (true){
            biblioteca.librarian.displayWelcomeMessage();
            biblioteca.librarian.displayMainMenu();
            switch (biblioteca.customer.selectOption()) {
                case 1:
                    if(biblioteca.user == null)
                        biblioteca.displayLoginStatus();
                    else
                        System.out.println("Already logged in...");
                    break;
                case 2:
                    biblioteca.librarian.displayAllBooks();
                    if (biblioteca.user != null) {
                        biblioteca.librarian.displayBookMenu();
                        switch (biblioteca.customer.selectOption()) {
                            case 1:
                                biblioteca.displayAvailabilityStatus();
                                break;
                            case 2:
                                biblioteca.displayReservationStatus();
                                break;
                            default:
                                System.out.println("Invalid option");
                            }
                    }
                    break;
                case 3:
                    biblioteca.librarian.displayAllMovies();
                    break;
                case 4:
                    biblioteca.librarian.checkMembership(biblioteca.user);
                    break;
                case 5:
                    System.exit(0);
                default:
                    System.out.println("Invalid option");
            }
        }
    }

    private void displayLoginStatus() throws IOException {
        System.out.println("Enter user name:");
        String name = customer.selectString();
        System.out.println("Enter password:");
        if(librarian.validateLogin(name, customer.selectString())) {
            System.out.println("Logged in successfully");
            user = new User(name, name+"@gmail.com");
        }
        else
            System.out.println("Invalid login! Try again...");
    }

    public void displayAvailabilityStatus() throws IOException {
        System.out.println("Enter Book name:");
        if (librarian.checkBookAvailability(customer.selectString()))
            System.out.println("Book available");
        else
            System.out.println("Book unavailable");
    }

    public void displayReservationStatus() throws IOException {
        System.out.println("Enter Book name:");
        if (librarian.reserveBook(customer.selectString()))
            System.out.println("Book reserved");
        else
            System.out.println("Book unreserved");
    }
}
